home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / CDOCUMENTTEMPLATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-13  |  24.3 KB  |  977 lines

  1. /*****************************************************************************
  2.   
  3.   Zope Public License (ZPL) Version 1.0
  4.   -------------------------------------
  5.   
  6.   Copyright (c) Digital Creations.  All rights reserved.
  7.   
  8.   This license has been certified as Open Source(tm).
  9.   
  10.   Redistribution and use in source and binary forms, with or without
  11.   modification, are permitted provided that the following conditions are
  12.   met:
  13.   
  14.   1. Redistributions in source code must retain the above copyright
  15.      notice, this list of conditions, and the following disclaimer.
  16.   
  17.   2. Redistributions in binary form must reproduce the above copyright
  18.      notice, this list of conditions, and the following disclaimer in
  19.      the documentation and/or other materials provided with the
  20.      distribution.
  21.   
  22.   3. Digital Creations requests that attribution be given to Zope
  23.      in any manner possible. Zope includes a "Powered by Zope"
  24.      button that is installed by default. While it is not a license
  25.      violation to remove this button, it is requested that the
  26.      attribution remain. A significant investment has been put
  27.      into Zope, and this effort will continue if the Zope community
  28.      continues to grow. This is one way to assure that growth.
  29.   
  30.   4. All advertising materials and documentation mentioning
  31.      features derived from or use of this software must display
  32.      the following acknowledgement:
  33.   
  34.        "This product includes software developed by Digital Creations
  35.        for use in the Z Object Publishing Environment
  36.        (http://www.zope.org/)."
  37.   
  38.      In the event that the product being advertised includes an
  39.      intact Zope distribution (with copyright and license included)
  40.      then this clause is waived.
  41.   
  42.   5. Names associated with Zope or Digital Creations must not be used to
  43.      endorse or promote products derived from this software without
  44.      prior written permission from Digital Creations.
  45.   
  46.   6. Modified redistributions of any form whatsoever must retain
  47.      the following acknowledgment:
  48.   
  49.        "This product includes software developed by Digital Creations
  50.        for use in the Z Object Publishing Environment
  51.        (http://www.zope.org/)."
  52.   
  53.      Intact (re-)distributions of any official Zope release do not
  54.      require an external acknowledgement.
  55.   
  56.   7. Modifications are encouraged but must be packaged separately as
  57.      patches to official Zope releases.  Distributions that do not
  58.      clearly separate the patches from the original work must be clearly
  59.      labeled as unofficial distributions.  Modifications which do not
  60.      carry the name Zope may be packaged in any form, as long as they
  61.      conform to all of the clauses above.
  62.   
  63.   
  64.   Disclaimer
  65.   
  66.     THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  67.     EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68.     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  69.     PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  70.     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  71.     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  72.     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  73.     USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  74.     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  75.     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  76.     OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  77.     SUCH DAMAGE.
  78.   
  79.   
  80.   This software consists of contributions made by Digital Creations and
  81.   many individuals on behalf of Digital Creations.  Specific
  82.   attributions are listed in the accompanying credits file.
  83.   
  84.  ****************************************************************************/
  85. static char cDocumentTemplate_module_documentation[] = 
  86. ""
  87. "\n$Id: cDocumentTemplate.c,v 1.32.18.2 2000/11/13 16:11:20 brian Exp $"
  88. ;
  89.  
  90. #include "ExtensionClass.h"
  91.  
  92. static PyObject *py_isDocTemp=0, *py_blocks=0, *py_=0, *join=0, *py_acquire;
  93. static PyObject *py___call__, *py___roles__, *py_AUTHENTICATED_USER;
  94. static PyObject *py_hasRole, *py__proxy_roles, *py_Unauthorized;
  95. static PyObject *py_Unauthorized_fmt, *py_validate;
  96. static PyObject *py__push, *py__pop, *py_aq_base;
  97.  
  98. /* ----------------------------------------------------- */
  99.  
  100. static void PyVar_Assign(PyObject **v, PyObject *e) { Py_XDECREF(*v); *v=e;}
  101. #define ASSIGN(V,E) PyVar_Assign(&(V),(E))
  102. #define UNLESS(E) if (!(E))
  103. #define UNLESS_ASSIGN(V,E) ASSIGN(V,E); UNLESS(V)
  104. #define OBJECT(O)(((PyObject*)O))
  105.  
  106. typedef struct {
  107.   PyObject_HEAD
  108.   PyObject *inst;
  109.   PyObject *cache;
  110.   PyObject *namespace;
  111.   PyObject *validate;
  112. } InstanceDictobject;
  113.  
  114. staticforward PyExtensionClass InstanceDictType;
  115.  
  116. static PyObject *
  117. InstanceDict___init__(InstanceDictobject *self, PyObject *args)
  118. {
  119.   self->validate=NULL;
  120.   UNLESS(PyArg_ParseTuple(args, "OO|O",
  121.               &(self->inst),
  122.               &(self->namespace),
  123.               &(self->validate)))
  124.     return NULL;
  125.   Py_INCREF(self->inst);
  126.   Py_INCREF(self->namespace);
  127.   if (self->validate)
  128.     Py_INCREF(self->validate);
  129.   else
  130.     UNLESS(self->validate=PyObject_GetAttr(self->namespace, py_validate))
  131.        return NULL;
  132.     
  133.   UNLESS(self->cache=PyDict_New()) return NULL;
  134.   Py_INCREF(Py_None);
  135.   return Py_None;
  136. }
  137.  
  138. static struct PyMethodDef InstanceDict_methods[] = {
  139.   {"__init__",    (PyCFunction)InstanceDict___init__, 1,
  140.    ""},
  141.   
  142.   {NULL,        NULL}        /* sentinel */
  143. };
  144.  
  145. /* ---------- */
  146.  
  147. static void
  148. InstanceDict_dealloc(InstanceDictobject *self)
  149. {
  150.   Py_XDECREF(self->inst);
  151.   Py_XDECREF(self->cache);
  152.   Py_XDECREF(self->namespace);
  153.   Py_XDECREF(self->validate);
  154.   Py_DECREF(self->ob_type);
  155.   PyMem_DEL(self);
  156. }
  157.  
  158. static PyObject *
  159. InstanceDict_getattr(InstanceDictobject *self, PyObject *name)
  160. {
  161.   return Py_FindAttr((PyObject *)self, name);
  162. }
  163.  
  164. static PyObject *
  165. InstanceDict_repr(InstanceDictobject *self)
  166. {
  167.   return PyObject_Repr(self->inst);
  168. }
  169.  
  170. /* Code to access InstanceDict objects as mappings */
  171.  
  172. static int
  173. InstanceDict_length( InstanceDictobject *self)
  174. {
  175.   return 1;
  176. }
  177.  
  178. static PyObject *
  179. InstanceDict_subscript( InstanceDictobject *self, PyObject *key)
  180. {
  181.   PyObject *r, *v;
  182.   char *name;
  183.   
  184.   /* Try to get value from the cache */
  185.   if (r=PyObject_GetItem(self->cache, key)) return r;
  186.   PyErr_Clear();
  187.   
  188.   /* Check for __str__ */
  189.   UNLESS(name=PyString_AsString(key)) return NULL;
  190.   if (*name=='_')
  191.     {
  192.       UNLESS(strcmp(name,"__str__")==0) goto KeyError;
  193.       return PyObject_Str(self->inst);
  194.     }
  195.   
  196.   /* Do explicit acquisition with "roles" rule */
  197.   if (r=PyObject_GetAttr(self->inst, py_acquire))
  198.     {
  199.       /* Sanity check in case of explicit Aq */
  200.       if (v=PyObject_GetAttr(self->inst, key)) Py_DECREF(v);  
  201.       else 
  202.     {
  203.       Py_DECREF(r);
  204.       goto KeyError;
  205.     }
  206.  
  207.       if (self->validate != Py_None)
  208.     {
  209.       UNLESS_ASSIGN(r,PyObject_CallFunction(
  210.          r, "OOO", key, self->validate, self->namespace))
  211.         {
  212.           PyObject *tb;
  213.  
  214.           PyErr_Fetch(&r, &v, &tb);
  215.           if (r != PyExc_AttributeError || PyObject_Compare(v,key))
  216.         {
  217.           PyErr_Restore(r,v,tb);
  218.           return NULL;
  219.         }
  220.           Py_XDECREF(r);
  221.           Py_XDECREF(v);
  222.           Py_XDECREF(tb);
  223.           
  224.           goto KeyError;
  225.         }
  226.     }
  227.       else
  228.     UNLESS_ASSIGN(r, PyObject_GetAttr(self->inst, key)) goto KeyError;
  229.     }  
  230.   else
  231.     {
  232.       PyErr_Clear();
  233.  
  234.       /* OK, use getattr */
  235.       UNLESS(r=PyObject_GetAttr(self->inst, key)) goto KeyError;
  236.  
  237.       if (self->validate != Py_None)
  238.     {
  239.       UNLESS(v=PyObject_CallFunction(
  240.         self->validate,"OOOOO",
  241.         self->inst, self->inst, key, r, self->namespace))
  242.         return NULL;
  243.       Py_DECREF(v);
  244.     }
  245.     }
  246.   
  247.   if (r && PyObject_SetItem(self->cache, key, r) < 0) PyErr_Clear();
  248.   
  249.   return r;
  250.   
  251. KeyError:
  252.   PyErr_SetObject(PyExc_KeyError, key);
  253.   return NULL;
  254. }
  255.  
  256. static int
  257. InstanceDict_ass_sub( InstanceDictobject *self, PyObject *v, PyObject *w)
  258. {
  259.   PyErr_SetString(PyExc_TypeError,
  260.           "InstanceDict objects do not support item assignment");
  261.   return -1;
  262. }
  263.  
  264. static PyMappingMethods InstanceDict_as_mapping = {
  265.   (inquiry)InstanceDict_length,        /*mp_length*/
  266.   (binaryfunc)InstanceDict_subscript,        /*mp_subscript*/
  267.   (objobjargproc)InstanceDict_ass_sub,    /*mp_ass_subscript*/
  268. };
  269.  
  270. /* -------------------------------------------------------- */
  271.  
  272.  
  273. static char InstanceDicttype__doc__[] = 
  274. ""
  275. ;
  276.  
  277. static PyExtensionClass InstanceDictType = {
  278.   PyObject_HEAD_INIT(NULL)
  279.   0,                /*ob_size*/
  280.   "InstanceDict",            /*tp_name*/
  281.   sizeof(InstanceDictobject),    /*tp_basicsize*/
  282.   0,                /*tp_itemsize*/
  283.   /* methods */
  284.   (destructor)InstanceDict_dealloc,    /*tp_dealloc*/
  285.   (printfunc)0,    /*tp_print*/
  286.   (getattrfunc)0,        /*obsolete tp_getattr*/
  287.   (setattrfunc)0,        /*obsolete tp_setattr*/
  288.   (cmpfunc)0,    /*tp_compare*/
  289.   (reprfunc)InstanceDict_repr,        /*tp_repr*/
  290.   0,        /*tp_as_number*/
  291.   0,        /*tp_as_sequence*/
  292.   &InstanceDict_as_mapping,        /*tp_as_mapping*/
  293.   (hashfunc)0,        /*tp_hash*/
  294.   (ternaryfunc)0,    /*tp_call*/
  295.   (reprfunc)0,        /*tp_str*/
  296.   (getattrofunc)InstanceDict_getattr,            /*tp_getattro*/
  297.   0,            /*tp_setattro*/
  298.   
  299.   /* Space for future expansion */
  300.   0L,0L,
  301.   InstanceDicttype__doc__, /* Documentation string */
  302.   METHOD_CHAIN(InstanceDict_methods)
  303. };
  304.  
  305. typedef struct {
  306.   PyObject_HEAD
  307.   int level;
  308.   PyObject *dict;
  309.   PyObject *data;
  310. } MM;
  311.  
  312. staticforward PyExtensionClass MMtype;
  313.  
  314. static PyObject *
  315. MM_push(self, args)
  316.     MM *self;
  317.     PyObject *args;
  318. {
  319.   PyObject *src;
  320.   UNLESS(PyArg_Parse(args, "O", &src)) return NULL;
  321.   UNLESS(-1 != PyList_Append(self->data,src)) return NULL;
  322.   Py_INCREF(Py_None);
  323.   return Py_None;
  324. }
  325.  
  326. static PyObject *
  327. MM_pop(self, args)
  328.     MM *self;
  329.     PyObject *args;
  330. {
  331.   int i=1, l;
  332.   PyObject *r;
  333.  
  334.   if (args) UNLESS(PyArg_Parse(args, "i", &i)) return NULL;
  335.   if ((l=PyList_Size(self->data)) < 0) return NULL;
  336.   i=l-i;
  337.   UNLESS(r=PySequence_GetItem(self->data,l-1)) return NULL;
  338.   if (PyList_SetSlice(self->data,i,l,NULL) < 0) goto err;
  339.   return r;
  340. err:
  341.   Py_DECREF(r);
  342.   return NULL;
  343. }
  344.  
  345. static PyObject *
  346. MM__init__(self, args)
  347.      MM *self;
  348.      PyObject *args;
  349. {
  350.   UNLESS(PyArg_Parse(args, "")) return NULL;
  351.   UNLESS(self->data=PyList_New(0)) return NULL;
  352.   self->dict=NULL;
  353.   self->level=0;
  354.   Py_INCREF(Py_None);
  355.   return Py_None;
  356. }
  357.  
  358.  
  359.  
  360. static int 
  361. dtObjectIsCallable(PyObject *ob) {
  362.   PyObject *base=0;
  363.   int result=0;
  364.  
  365.   /* Ensure that an object is really callable by unwrapping it */
  366.   UNLESS(base=PyObject_GetAttr(ob, py_aq_base)) {
  367.     PyErr_Clear();
  368.     return PyCallable_Check(ob);
  369.   }
  370.   result=PyCallable_Check(base);
  371.   Py_DECREF(base);
  372.   return result;
  373. }
  374.  
  375. static int
  376. dtObjectIsDocTemp(PyObject *ob) {
  377.   PyObject *base=0;
  378.   PyObject *value=0;
  379.   int result=0;
  380.  
  381.   /* Ensure that 'isDocTemp' is not acquired */
  382.   UNLESS(base=PyObject_GetAttr(ob, py_aq_base)) {
  383.     PyErr_Clear();
  384.     base = ob;
  385.     Py_INCREF(base);
  386.   }
  387.  
  388.   if ( value = PyObject_GetAttr(base, py_isDocTemp) ) {
  389.     if (PyObject_IsTrue(value)) {
  390.       result = 1;
  391.     }
  392.     Py_DECREF(value);
  393.   }
  394.   else PyErr_Clear();
  395.  
  396.   Py_DECREF(base);
  397.   return result;
  398. }
  399.  
  400.  
  401. static PyObject *
  402. MM_cget(MM *self, PyObject *key, int call)
  403. {
  404.   long i;
  405.   int dt=0;
  406.   PyObject *e, *t, *rr, *tb;
  407.  
  408.   UNLESS(-1 != (i=PyList_Size(self->data))) return NULL;
  409.   while (--i >= 0)
  410.     {
  411.       e=PyList_GetItem(self->data,i);
  412.       if (e=PyObject_GetItem(e,key))
  413.     {
  414.       dt=0;
  415.  
  416.  
  417.  
  418.       /*      if (PyCallable_Check(e)) */
  419.  
  420.       if (dtObjectIsCallable(e))
  421.         {
  422.  
  423.           
  424.           /* Decide whether we have a document template 
  425.           if (rr=PyObject_GetAttr(e,py_isDocTemp))
  426.         {
  427.           if (PyObject_IsTrue(rr)) dt=1;
  428.           Py_DECREF(rr);
  429.         }
  430.           else PyErr_Clear();
  431.           */
  432.  
  433.           dt = dtObjectIsDocTemp(e);
  434.  
  435.  
  436.           /* Try calling the object */
  437.           if (call)
  438.         {
  439.           if (dt)
  440.             {
  441.               ASSIGN(e,PyObject_CallFunction(e,"OO", Py_None, self));
  442.               UNLESS(e)
  443.             return NULL;
  444.             }
  445.           else
  446.             {
  447.               rr=PyObject_CallObject(e,NULL);
  448.               if (rr) ASSIGN(e,rr);
  449.               else
  450.             {
  451.               PyErr_Fetch(&t, &rr, &tb);
  452.               if (t!=PyExc_AttributeError ||
  453.                  PyObject_Compare(rr,py___call__) != 0)
  454.                 {
  455.                   PyErr_Restore(t,rr,tb);
  456.                   Py_DECREF(e);
  457.                   return NULL;
  458.                 }
  459.               /* 
  460.                             Added by Brian on 08/30/99. We need to be sure
  461.                             to DECREF the exception in the event of an 
  462.                             AttributeError to avoid leaking.
  463.                           */
  464.               else {
  465.                 Py_XDECREF(t);
  466.                 Py_XDECREF(rr);
  467.                 Py_XDECREF(tb);
  468.               }
  469.  
  470.             }
  471.             }
  472.         }
  473.         }
  474.  
  475.       return e;
  476.     }
  477.       PyErr_Fetch(&e, &rr, &tb);
  478.       if (dt || e != PyExc_KeyError)
  479.     {
  480.       PyErr_Restore(e,rr,tb);
  481.       return NULL;
  482.     }
  483.       Py_XDECREF(e);
  484.       Py_XDECREF(rr);
  485.       Py_XDECREF(tb);
  486.     }
  487.   PyErr_SetObject(PyExc_KeyError,key);
  488.   return NULL;
  489. }
  490.  
  491. static PyObject *
  492. MM_get(MM *self, PyObject *args)
  493. {
  494.   PyObject *key, *call=Py_None;
  495.  
  496.   UNLESS(PyArg_ParseTuple(args,"O|O",&key,&call)) return NULL;
  497.   return MM_cget(self, key, PyObject_IsTrue(call));
  498. }
  499.  
  500. static PyObject *
  501. MM_has_key(MM *self, PyObject *args)
  502. {
  503.   PyObject *key;
  504.  
  505.   UNLESS(PyArg_ParseTuple(args,"O",&key)) return NULL;
  506.   if ((key=MM_cget(self, key, 0)))
  507.     {
  508.       Py_DECREF(key);
  509.       return PyInt_FromLong(1);
  510.     }
  511.   PyErr_Clear();
  512.   return PyInt_FromLong(0);
  513. }
  514.  
  515. static struct PyMethodDef MM_methods[] = {
  516.   {"__init__", (PyCFunction)MM__init__, 0,
  517.    "__init__() -- Create a new empty multi-mapping"},
  518.   {"_push", (PyCFunction) MM_push, 0,
  519.    "_push(mapping_object) -- Add a data source"},
  520.   {"_pop",  (PyCFunction) MM_pop,  0,
  521.    "_pop() -- Remove and return the last data source added"}, 
  522.   {"getitem",  (PyCFunction) MM_get,  METH_VARARGS,
  523.    "getitem(key[,call]) -- Get a value\n\n"
  524.    "Normally, callable objects that can be called without arguments are\n"
  525.    "called during retrieval. This can be suppressed by providing a\n"
  526.    "second argument that is false.\n"
  527.   }, 
  528.   {"has_key",  (PyCFunction) MM_has_key,  METH_VARARGS,
  529.    "has_key(key) -- Test whether the mapping has the given key"
  530.   }, 
  531.   {NULL,        NULL}        /* sentinel */
  532. };
  533.  
  534. static void
  535. MM_dealloc(self)
  536.      MM *self;
  537. {
  538.   Py_XDECREF(self->data);
  539.   Py_XDECREF(self->dict);
  540.   Py_DECREF(self->ob_type);
  541.   PyMem_DEL(self);
  542. }
  543.  
  544. static PyObject *
  545. MM_getattro(MM *self, PyObject *name)
  546. {
  547.   if (PyString_Check(name))
  548.     {
  549.       if (strcmp(PyString_AsString(name),"level")==0)
  550.     return PyInt_FromLong(self->level);
  551.     }
  552.   
  553.   if (self->dict)
  554.     {
  555.       PyObject *v;
  556.  
  557.       if (v=PyDict_GetItem(self->dict, name))
  558.     {
  559.       Py_INCREF(v);
  560.       return v;
  561.     }
  562.     }
  563.   
  564.   return Py_FindAttr((PyObject *)self, name);
  565. }
  566.  
  567. static int
  568. MM_setattro(MM *self, PyObject *name, PyObject *v)
  569. {
  570.   if (v && PyString_Check(name))
  571.     {
  572.       if (strcmp(PyString_AsString(name),"level")==0)
  573.     {
  574.       self->level=PyInt_AsLong(v);
  575.       if (PyErr_Occurred()) return -1;
  576.       return 0;
  577.     }
  578.     }
  579.  
  580.   if (! self->dict && ! (self->dict=PyDict_New())) return -1;
  581.   
  582.   if (v) return PyDict_SetItem(self->dict, name, v);
  583.   else  return PyDict_DelItem(self->dict, name);
  584. }
  585.  
  586. static int
  587. MM_length(self)
  588.     MM *self;
  589. {
  590.   long l=0, el, i;
  591.   PyObject *e=0;
  592.  
  593.   UNLESS(-1 != (i=PyList_Size(self->data))) return -1;
  594.   while (--i >= 0)
  595.     {
  596.       e=PyList_GetItem(self->data,i);
  597.       UNLESS(-1 != (el=PyObject_Length(e))) return -1;
  598.       l+=el;
  599.     }
  600.   return l;
  601. }
  602.  
  603. static PyObject *
  604. MM_subscript(MM *self, PyObject *key)
  605. {
  606.   return MM_cget(self, key, 1);
  607. }
  608.  
  609. typedef struct {
  610.   PyObject_HEAD
  611.   PyObject *data;
  612. } DictInstance;
  613.  
  614. static void
  615. DictInstance_dealloc(DictInstance *self)
  616. {
  617.   Py_DECREF(self->data);
  618.   PyMem_DEL(self);
  619. }
  620.  
  621. static PyObject *
  622. DictInstance_getattr(DictInstance *self, PyObject *name)
  623. {
  624.   PyObject *r;
  625.  
  626.   if ((r=PyObject_GetItem(self->data, name))) return r;
  627.   PyErr_SetObject(PyExc_AttributeError, name);
  628.   return NULL;
  629. }
  630.  
  631. static PyTypeObject DictInstanceType = {
  632.   PyObject_HEAD_INIT(NULL)
  633.   0,                /*ob_size*/
  634.   "DictInstance",            /*tp_name*/
  635.   sizeof(DictInstance),        /*tp_basicsize*/
  636.   0,                /*tp_itemsize*/
  637.   (destructor)DictInstance_dealloc,
  638.   (printfunc)0,
  639.   (getattrfunc)0,
  640.   (setattrfunc)0,
  641.   (cmpfunc)0,
  642.   (reprfunc)0,
  643.   0, 0, 0,
  644.   (hashfunc)0,
  645.   (ternaryfunc)0,
  646.   (reprfunc)0,
  647.   (getattrofunc)DictInstance_getattr,
  648.   (setattrofunc)0,
  649.   0L,0L,
  650.   "Wrap a mapping object to look like an instance"
  651. };
  652.  
  653. static DictInstance *
  654. newDictInstance(PyObject *data)
  655. {
  656.   DictInstance *self;
  657.     
  658.   UNLESS(self = PyObject_NEW(DictInstance, &DictInstanceType)) return NULL;
  659.   self->data=data;
  660.   Py_INCREF(data);
  661.   return self;
  662. }
  663.  
  664. static PyObject *
  665. MM_call(MM *self, PyObject *args, PyObject *kw)
  666. {
  667.   PyObject *r, *t;
  668.   int i, l=0;
  669.  
  670.   if (args && (l=PyTuple_Size(args)) < 0) return NULL;
  671.   if (l)
  672.     {
  673.       UNLESS(r=PyObject_CallObject(OBJECT(self->ob_type), NULL)) return NULL;
  674.       for (i=0; i < l; i++) 
  675.     if (PyList_Append(((MM*)r)->data, PyTuple_GET_ITEM(args, i)) < 0) 
  676.       goto err;
  677.       if (kw && PyList_Append(((MM*)r)->data, kw) < 0) goto err;
  678.     }
  679.   else
  680.     {
  681.       if (!kw) 
  682.     {
  683.       Py_INCREF(Py_None);
  684.       return Py_None;
  685.     }
  686.       r=kw;
  687.       Py_INCREF(r);
  688.     }
  689.  
  690.   ASSIGN(r, OBJECT(newDictInstance(r)));
  691.   UNLESS(t=PyTuple_New(1)) goto err;
  692.   PyTuple_SET_ITEM(t, 0, r);
  693.   return t;
  694.  
  695. err:
  696.   Py_XDECREF(r);
  697.   return NULL;
  698. }
  699.  
  700. static PyMappingMethods MM_as_mapping = {
  701.     (inquiry)MM_length,        /*mp_length*/
  702.     (binaryfunc)MM_subscript,          /*mp_subscript*/
  703.     (objobjargproc)NULL,        /*mp_ass_subscript*/
  704. };
  705.  
  706. /* -------------------------------------------------------- */
  707.  
  708. static char MMtype__doc__[] = 
  709. "TemplateDict -- Combine multiple mapping objects for lookup"
  710. ;
  711.  
  712. static PyExtensionClass MMtype = {
  713.     PyObject_HEAD_INIT(NULL)
  714.     0,                /*ob_size*/
  715.     "TemplateDict",            /*tp_name*/
  716.     sizeof(MM),            /*tp_basicsize*/
  717.     0,                /*tp_itemsize*/
  718.     /* methods */
  719.     (destructor)MM_dealloc,        /*tp_dealloc*/
  720.     (printfunc)0,            /*tp_print*/
  721.     (getattrfunc)0,            /*tp_getattr*/
  722.     (setattrfunc)0,            /*tp_setattr*/
  723.     (cmpfunc)0,            /*tp_compare*/
  724.     (reprfunc)0,            /*tp_repr*/
  725.     0,                /*tp_as_number*/
  726.     0,                /*tp_as_sequence*/
  727.     &MM_as_mapping,            /*tp_as_mapping*/
  728.     (hashfunc)0,            /*tp_hash*/
  729.     (ternaryfunc)MM_call,        /*tp_call*/
  730.     (reprfunc)0,            /*tp_str*/
  731.     (getattrofunc)MM_getattro,    /*tp_getattro*/
  732.     (setattrofunc)MM_setattro,    /*tp_setattro*/
  733.  
  734.     /* Space for future expansion */
  735.     0L,0L,
  736.     MMtype__doc__, /* Documentation string */
  737.     METHOD_CHAIN(MM_methods)
  738. };
  739.  
  740. static struct PyMethodDef TemplateDict_methods[] = {
  741.   {NULL,        NULL}        /* sentinel */
  742. };
  743.  
  744.  
  745.  
  746. /* List of methods defined in the module */
  747.  
  748. static int
  749. if_finally(PyObject *md, int err)
  750. {
  751.   PyObject *t, *v, *tb;
  752.  
  753.   if (err) PyErr_Fetch(&t, &v, &tb);
  754.  
  755.   md=PyObject_GetAttr(md,py__pop);
  756.   if (md) ASSIGN(md, PyObject_CallObject(md,NULL));
  757.   
  758.   if (err) PyErr_Restore(t,v,tb);
  759.   
  760.   if (md)
  761.     {
  762.       Py_DECREF(md);
  763.       return -1;
  764.     }
  765.   else 
  766.     return -2;
  767. }
  768.  
  769. static int
  770. render_blocks_(PyObject *blocks, PyObject *rendered,
  771.            PyObject *md, PyObject *mda)
  772. {
  773.   PyObject *block;
  774.   int l, i, k=0, append;
  775.  
  776.   if ((l=PyList_Size(blocks)) < 0) return -1;
  777.   for (i=0; i < l; i++)
  778.     {
  779.       block=PyList_GET_ITEM(((PyListObject*)blocks), i);
  780.       append=1;
  781.  
  782.       if (PyTuple_Check(block))
  783.     {
  784.       int bs;
  785.  
  786.       bs=((PyTupleObject*)block)->ob_size;
  787.       
  788.       if (bs==1)
  789.         {
  790.           /* Simple var */
  791.           block=PyTuple_GET_ITEM(block,0);
  792.           if (PyString_Check(block)) block=PyObject_GetItem(md,block);
  793.           else block=PyObject_CallObject(block,mda);
  794.           if (block) ASSIGN(block, PyObject_Str(block));
  795.           UNLESS(block) return -1;
  796.         }
  797.       else
  798.         {
  799.           /* if */
  800.           int icond, m;
  801.           PyObject *cond, *n, *cache;
  802.  
  803.           UNLESS(cache=PyDict_New()) return -1;
  804.           cond=PyObject_GetAttr(md,py__push);
  805.           if (cond) ASSIGN(cond, PyObject_CallFunction(cond,"O",cache));
  806.           Py_DECREF(cache);
  807.           if (cond) Py_DECREF(cond);
  808.           else return -1;
  809.           
  810.           append=0;
  811.           m=bs-1;
  812.           for (icond=0; icond < m; icond += 2)
  813.         {
  814.           cond=PyTuple_GET_ITEM(block,icond);
  815.           if (PyString_Check(cond))
  816.             {
  817.               /* We have to be careful to handle key errors here */
  818.               n=cond;
  819.               if (cond=PyObject_GetItem(md,cond))
  820.             {
  821.               if (PyDict_SetItem(cache, n, cond) < 0)
  822.                 {
  823.                   Py_DECREF(cond);
  824.                   return if_finally(md,1);
  825.                 }
  826.             }
  827.               else
  828.             {
  829.               PyObject *t, *v, *tb;
  830.  
  831.               PyErr_Fetch(&t, &v, &tb);
  832.               if (t != PyExc_KeyError || PyObject_Compare(v,n))
  833.                 {
  834.                   PyErr_Restore(t,v,tb);
  835.                   return if_finally(md,1);
  836.                 }
  837.               Py_XDECREF(t);
  838.               Py_XDECREF(v);
  839.               Py_XDECREF(tb);
  840.               cond=Py_None;
  841.               Py_INCREF(cond);
  842.             }
  843.             }
  844.           else
  845.             UNLESS(cond=PyObject_CallObject(cond,mda))
  846.                return if_finally(md,1);
  847.  
  848.           if (PyObject_IsTrue(cond))
  849.             {
  850.               Py_DECREF(cond);
  851.               block=PyTuple_GET_ITEM(block,icond+1);
  852.               if (block!=Py_None &&
  853.              render_blocks_(block, rendered, md, mda) < 0)
  854.             return if_finally(md,1);
  855.               m=-1;
  856.               break;
  857.             }
  858.           else Py_DECREF(cond);
  859.         }
  860.         if (icond==m)
  861.           {
  862.             block=PyTuple_GET_ITEM(block,icond);
  863.             if (block!=Py_None &&
  864.                render_blocks_(block, rendered, md, mda) < 0)
  865.               return if_finally(md,1);
  866.           }
  867.  
  868.         if (if_finally(md,0) == -2) return -1;
  869.         }
  870.     }
  871.       else if (PyString_Check(block))
  872.     {
  873.       Py_INCREF(block);
  874.     }
  875.       else
  876.     {
  877.       UNLESS(block=PyObject_CallObject(block,mda)) return -1;
  878.     }
  879.  
  880.       if (append && PyObject_IsTrue(block))
  881.     {
  882.       k=PyList_Append(rendered,block);
  883.       Py_DECREF(block);
  884.       if (k < 0) return -1;
  885.     }
  886.     }
  887.  
  888.   return 0;
  889. }
  890.  
  891. static PyObject *
  892. render_blocks(PyObject *self, PyObject *args)
  893. {
  894.   PyObject *md, *blocks, *mda=0, *rendered=0;
  895.   int l;
  896.  
  897.   UNLESS(PyArg_ParseTuple(args,"OO", &blocks, &md)) return NULL;
  898.   UNLESS(rendered=PyList_New(0)) goto err;
  899.   UNLESS(mda=Py_BuildValue("(O)",md)) goto err;
  900.   
  901.   if (render_blocks_(blocks, rendered, md, mda) < 0) goto err;
  902.  
  903.   Py_DECREF(mda);
  904.  
  905.   l=PyList_Size(rendered);
  906.   if (l==0)
  907.     {
  908.       Py_INCREF(py_);
  909.       ASSIGN(rendered, py_);
  910.     }
  911.   else if (l==1)
  912.     ASSIGN(rendered, PySequence_GetItem(rendered,0));
  913.   else
  914.     ASSIGN(rendered, PyObject_CallFunction(join,"OO",rendered,py_));
  915.  
  916.   return rendered;
  917.  
  918. err:
  919.   Py_XDECREF(mda);
  920.   Py_XDECREF(rendered);
  921.   return NULL;
  922. }  
  923.   
  924. static struct PyMethodDef Module_Level__methods[] = {
  925.   {"render_blocks", (PyCFunction)render_blocks,    METH_VARARGS,
  926.    ""},
  927.   {NULL, (PyCFunction)NULL, 0, NULL}        /* sentinel */
  928. };
  929.  
  930. void
  931. initcDocumentTemplate()
  932. {
  933.   PyObject *m, *d;
  934.   char *rev="$Revision: 1.32.18.2 $";
  935.  
  936.   DictInstanceType.ob_type=&PyType_Type;
  937.  
  938.   UNLESS(py_isDocTemp=PyString_FromString("isDocTemp")) return;
  939.   UNLESS(py_blocks=PyString_FromString("blocks")) return;
  940.   UNLESS(py_acquire=PyString_FromString("aq_acquire")) return;
  941.   UNLESS(py___call__=PyString_FromString("__call__")) return;
  942.   UNLESS(py___roles__=PyString_FromString("__roles__")) return;
  943.   UNLESS(py__proxy_roles=PyString_FromString("_proxy_roles")) return;
  944.   UNLESS(py_hasRole=PyString_FromString("hasRole")) return;
  945.   UNLESS(py_validate=PyString_FromString("validate")) return;
  946.   UNLESS(py__push=PyString_FromString("_push")) return;
  947.   UNLESS(py__pop=PyString_FromString("_pop")) return;
  948.   UNLESS(py_aq_base=PyString_FromString("aq_base")) return;
  949.   UNLESS(py_Unauthorized=PyString_FromString("Unauthorized")) return;
  950.   UNLESS(py_Unauthorized_fmt=PyString_FromString(
  951.      "You are not authorized to access <em>%s</em>.")) return;
  952.  
  953.   UNLESS(py_AUTHENTICATED_USER=PyString_FromString("AUTHENTICATED_USER"))
  954.     return;
  955.  
  956.   UNLESS(py_=PyString_FromString("")) return;
  957.   UNLESS(join=PyImport_ImportModule("string")) return;
  958.   ASSIGN(join,PyObject_GetAttrString(join,"join"));
  959.   UNLESS(join) return;
  960.   UNLESS(ExtensionClassImported) return;
  961.  
  962.   m = Py_InitModule4("cDocumentTemplate", Module_Level__methods,
  963.              cDocumentTemplate_module_documentation,
  964.              (PyObject*)NULL,PYTHON_API_VERSION);
  965.  
  966.   d = PyModule_GetDict(m);
  967.  
  968.   PyExtensionClass_Export(d,"InstanceDict",InstanceDictType);
  969.   PyExtensionClass_Export(d,"TemplateDict",MMtype);
  970.  
  971.   PyDict_SetItemString(d, "__version__",
  972.                PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
  973.  
  974.   if (PyErr_Occurred())
  975.     Py_FatalError("can't initialize module cDocumentTemplate");
  976. }
  977.